<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>我的帖子</title>
    <link rel="stylesheet" href="../css/my.css">
</head>
<body>
    <!-- 页面头部 -->
    <header>
        <div class="header-container">
            <h1 class="logo">学校论坛</h1>
            <nav class="navbar">
                <ul>
                    <li><a href="../index.jsp">首页</a></li>
                    <li><a href="studyExchange.jsp">学习交流</a></li>
                    <li><a href="campusLife.jsp">校园生活</a></li>
                    <li><a href="clubLife.jsp">社团活动</a></li>
                    <li><a href="post.jsp">发表帖子</a></li>
                    <li><a href="#">我的消息</a></li>
                </ul>
            </nav>
        </div>
    </header>

    <!-- 显示用户的所有帖子 -->
    <main>
        <h1>我的帖子</h1>

        <!-- 排序选择 -->
        <form method="get" action="myPosts.jsp">
            <label for="sortOption">排序方式: </label>
            <select name="sortOption" id="sortOption">
                <option value="time" <%= request.getParameter("sortOption") == null || request.getParameter("sortOption").equals("time") ? "selected" : "" %>>按时间排序</option>
                <option value="type" <%= request.getParameter("sortOption") != null && request.getParameter("sortOption").equals("type") ? "selected" : "" %>>按发布类型排序</option>
            </select>
            <input type="submit" value="排序">
        </form>
        <div class="btn-container">
            <a href="post.jsp" class="btn-add-post">增加帖子</a>
        </div>

        <%
            String username = session.getAttribute("username") != null ? session.getAttribute("username").toString() : "";
            String sortOption = request.getParameter("sortOption") != null ? request.getParameter("sortOption") : "time"; // 默认按时间排序

            if (!username.isEmpty()) {
                String DB_URL = "jdbc:mysql://localhost:3306/lczmysql?useUnicode=true&characterEncoding=UTF-8";
                String DB_USER = "root";
                String DB_PASSWORD = "123456";
                Class.forName("com.mysql.cj.jdbc.Driver");
                Connection connection = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
                
                // 根据选择的排序方式构建 SQL 查询
                String sql = "";
                if ("type".equals(sortOption)) {
                    sql = "SELECT * FROM post WHERE username = ? ORDER BY category ASC, post_time DESC";  // 按类型排序
                } else {
                    sql = "SELECT * FROM post WHERE username = ? ORDER BY post_time DESC";  // 按时间排序
                }

                PreparedStatement statement = connection.prepareStatement(sql);
                statement.setString(1, username);
                ResultSet resultSet = statement.executeQuery();

                while (resultSet.next()) {
        %>
        <article class="post-item">
            <h3><%= resultSet.getString("title") %></h3>
            <p>发布时间: <%= resultSet.getString("post_time") %></p>
            
            <!-- 处理 content，去除 HTML 标签并截取 -->
            <p class="excerpt">
                <%
                    String content = resultSet.getString("content");
                    if (content != null) {
                        // 使用正则去除 HTML 标签
                        String textOnly = content.replaceAll("<[^>]*>", ""); // 去除所有 HTML 标签
                        if (textOnly.length() > 100) {
                            out.print(textOnly.substring(0, 100) + "...");
                        } else {
                            out.print(textOnly); // 如果没有超过100个字符，直接输出
                        }
                    }
                %>
            </p>
   
            <a href="editPost.jsp?id=<%= resultSet.getInt("id") %>" class="btn-edit">编辑</a>
            <a href="deletePost.jsp?id=<%= resultSet.getInt("id") %>" class="btn-delete" onclick="return confirm('确定删除该帖子吗？')">删除</a>
        </article>
        <%
                }
                resultSet.close();
                statement.close();
                connection.close();
            } else {
                out.println("<p>没有登录，无法查看帖子。</p>");
            }
        %>
    </main>

    <!-- 页面底部 -->
    <footer>
        <p>&copy; 2024 学校论坛版权所有</p>
    </footer>
</body>
</html>
